import 'package:flutter/services.dart';
import 'package:enterprise_v8/app/components/custom_app_bar.dart';
import 'package:enterprise_v8/app/components/toast_widget.dart';
import 'package:enterprise_v8/app/core/core.dart';
import 'package:enterprise_v8/app/core/ext/widget_extension.dart';

import 'verify_control.dart';

/// 登陆后验证页面
/// 1. 手机号验证
/// 2. 邮箱验证
class VerifyPage extends StatelessWidget {
  const VerifyPage({super.key});

  @override
  Widget build(BuildContext context) {
    final control = Get.put(VerifyControl());
    return Scaffold(
      appBar: CustomAppBar(
        titleText: '验证手机号',
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            TextField(
              controller: control.phoneController,
              keyboardType: TextInputType.phone,
              inputFormatters: [FilteringTextInputFormatter.digitsOnly],
              decoration: const InputDecoration(labelText: '请输入手机号'),
            ),
            AppGaps.vGap16,
            Row(
              children: [
                Expanded(
                  child: TextField(
                    controller: control.codeController,
                    keyboardType: TextInputType.number,
                    inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                    decoration: const InputDecoration(labelText: '请输入验证码'),
                  ),
                ),
                SizedBox(
                  width: 100.w,
                  child: Obx(() => control.isSendingCode.value
                      ? Container(
                          alignment: Alignment.center,
                          child: Text(
                            '${control.secondsRemaining.value}s',
                            style: const TextStyle(color: Colors.grey),
                          ),
                        )
                      : TextButton(
                          onPressed: () {
                            if(control.phoneController.text.isEmpty) {
                              AppToast.showWarning('请输入手机号');
                              return;
                            }
                            if(control.phoneController.text.length != 11) {
                              AppToast.showWarning('请输入正确的手机号');
                              return;
                            }
                            control.isSendingCode.value = true;
                            control.startCountdown();
                            // 发送验证码逻辑
                          },
                          child: const Text('发送验证码'),
                        )),
                ),
              ],
            ),
            Expanded(child: Container()),
            Container(
              width: AppDimens.sw,
              height: 44.h,
              alignment: Alignment.center,
              decoration: BoxDecoration(color: const Color(0xff5C6DFF), borderRadius: BorderRadius.all(Radius.circular(44.w))),
              child: Text(
                '验证',
                style: TextStyle(
                  color: const Color(0xfff5f5f5),
                  fontWeight: FontWeight.w400,
                  fontSize: 18.sp,
                ),
              ),
            ).clickOnce(control.verify),
          ],
        ),
      ),
    );
  }
}
